package org.goyo.main.services.sale.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.sale.IDispatchDao;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.entity.sale.Dispatch;
import org.goyo.main.domain.entity.sale.DispatchEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.sale.IDispatchService;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class DispatchServiceImpl extends BaseService implements IDispatchService{
	@Resource
	private IDispatchDao dispatchDao;
	@Resource
	private ISaleOrderDao saleOrderDao;

	@Override
	public Pager<Dispatch> findByCondition(String where, Pager pager,Object[] groupUuids)
			throws DBException, ServiceException {
		
		return  dispatchDao.findByCond(where, pager,groupUuids);
	}

	@Override
	public Dispatch saveOrUpdateInfo(Dispatch info, Object[] uuids,SysUser user)
			throws DBException, ServiceException {
		String uuid = null;
		//保存用户数据
		info.setStatus(Constants.SAVESTATUS);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			info.setCreateDate(DateUtils.getCurrentDate());
			info.setCreateUser(user);
//			alterBillNumber(Dispatch.BIZTYPE, info.getNumber());
			uuid = dispatchDao.save(info);
		}else{
			dispatchDao.update(info);
			uuid = info.getUuid();
		}
		//删除数据
		if(uuids != null && uuids[0] != null){
			dispatchDao.delBatch(DispatchEntry.class, "uuid", uuids);
		}
		info.setUuid(uuid);
		return info;
	}

	@Override
	public List<DispatchEntry> findByParentId(String parentId) throws DBException,
			ServiceException {
		return dispatchDao.findByParentId(parentId);
	}
	
	
	
	@Override
	public void delByUuids(Object[] uuids) throws DBException, ServiceException {
		//批量删除
		for(Object uuid : uuids){
			Dispatch info = dispatchDao.findByUuid(Dispatch.class, uuid.toString());
			dispatchDao.delete(info);
		}
	}

	@Override
	public Dispatch submitByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		Dispatch info = null;
		//批量提交
		for(Object uuid : uuids){
			info = dispatchDao.findByUuid(Dispatch.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setSubmitDate(DateUtils.getCurrentDate());
			info.setSubmitUser(user);
			info.setStatus(Constants.SUBMITTATUS);
			dispatchDao.update(info);
		}
		return info;
	}

	@Override
	public Dispatch auditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		Dispatch info = null;
		//批量审核
		for(Object uuid : uuids){
			info = dispatchDao.findByUuid(Dispatch.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(DateUtils.getCurrentDate());
			info.setAuditUser(user);
			info.setStatus(Constants.AUDITSTATUS);
			dispatchDao.update(info);
			//找到销售订单反写
			updateSaleOrderDispQuty(info);
		}
		return info;
	}
	
	/**
	 * 反写销售订单累计发运数量
	 * 
	 * @author chenchuan
	 * 2014年12月5日
	 */
	public void updateSaleOrderDispQuty(Dispatch info)  throws DBException,ServiceException {
		List<DispatchEntry> entrys = info.getEntrys();
		for(DispatchEntry entry : entrys){
			String saleOrderEntryId = entry.getSaleOrderEntry().getUuid();
			saleOrderDao.updateSumDispQtyByUuid(saleOrderEntryId);
		}
	}
	@Override
	public Dispatch unAuditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		Dispatch info = null;
		//批量反审核
		for(Object uuid : uuids){
			info = dispatchDao.findByUuid(Dispatch.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(null);
			info.setAuditUser(null);
			info.setSubmitUser(null);
			info.setSubmitDate(null);
			info.setStatus(Constants.SAVESTATUS);
			dispatchDao.update(info);
		}
		return info;
	}

	@Override
	public Dispatch findByUuuid(String uuid) throws DBException,
			ServiceException {
		Dispatch info = dispatchDao.findByUuid(Dispatch.class, uuid);
		return info;
	}

	@Override
	public void updateByUuidLoss(String uuid, long lossQty, BigDecimal lossAmount)
			throws DBException, ServiceException {
		dispatchDao.updateByUuidLoss(uuid, lossQty, lossAmount);
	}

	@Override
	public Pager<DispatchEntry> findByEntryCondition(String where, Pager pager)
			throws DBException, ServiceException {
		pager = dispatchDao.findByEntryCond(where, pager);
//		List<DispatchEntry> entrys = pager.getList();
//		if(entrys != null && entrys.size() > 0){
//			Dispatch info = dispatchDao.get(Dispatch.class, entrys.get(0).getParentId());
//			for(DispatchEntry entry :entrys){
//				entry.setDispatch(info);
//			}
//		}
		return pager;
	}
}
